package org.platform.service.diagnose;

import org.apache.commons.lang3.StringUtils;
import org.platform.constant.ExecutorStatus;
import org.platform.dao.mapper.FdTimingDiagnoseStrategyMapper;
import org.platform.mo.csmo.cmd41012.AddTimingTaskRequestParam;
import org.platform.mo.csmo.cmd41012.AddTimingTaskResponseParam;
import org.platform.mo.csmo.cmd41013.ModifyTimingTaskRequestParam;
import org.platform.mo.csmo.cmd41013.ModifyTimingTaskResponseParam;
import org.platform.mo.csmo.cmd41014.QueryTimingTaskRequestParam;
import org.platform.mo.csmo.cmd41014.QueryTimingTaskResponseParam;
import org.platform.mo.csmo.cmd41015.DelTimingTaskRequestParam;
import org.platform.mo.csmo.cmd41015.DelTimingTaskResponseParam;
import org.platform.mo.csmo.common.Fd_TimingDiagnoseStrategy;
import org.platform.mo.db.FdTimingDiagnoseStrategy;
import org.platform.processor.diagnose.TimingTaskStrategyProcessor;
import org.platform.service.IBaseService;
import org.platform.tool.helper.LogHelper;
import org.platform.tool.util.BeansUtils;
import org.platform.tool.util.SerialNumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 定时诊断任务
 * 1.根据策略生成定时任务 {@link TimingTaskStrategyProcessor#addTiming(Long, Long, Integer, Integer, String)}
 * 2.持久化到数据库
 *
 * @author FengJie
 * @date 2018/8/16
 */
@Service
public class TimingDiagnoseTaskServiceImpl implements IBaseService {
    @Autowired
    private FdTimingDiagnoseStrategyMapper strategyMapper;
    @Autowired
    private TimingTaskStrategyProcessor timing;
    @Autowired
    private LogHelper logHelper;


    /**
     * 创建定时诊断任务(41012)
     */
    @Transactional(rollbackFor = Exception.class)
    public AddTimingTaskResponseParam addTimingTask(AddTimingTaskRequestParam requestParam) {

        Fd_TimingDiagnoseStrategy timingDiagnoseTask = requestParam.getTimingDiagnoseTask();
        String id = "TimingTask_" + SerialNumberUtils.getInstance().getSerialNumber();
        try {
            //保存到数据库
            FdTimingDiagnoseStrategy fdTimingDiagnoseTask = new FdTimingDiagnoseStrategy();
            BeanUtils.copyProperties(timingDiagnoseTask, fdTimingDiagnoseTask);
            fdTimingDiagnoseTask.setId(id);
            fdTimingDiagnoseTask.setExecutorStatus(ExecutorStatus.notExecutor.getValue());
            strategyMapper.insertSelective(fdTimingDiagnoseTask);

            //创建定时任务
            timing.addTiming(timingDiagnoseTask.getStartTime(),
                    timingDiagnoseTask.getEndTime(),
                    timingDiagnoseTask.getCycle(),
                    timingDiagnoseTask.getCount(),
                    id);
            logHelper.normal("创建定时诊断任务:" + timingDiagnoseTask.getTestName() + "【成功】", null, null);
        } catch (RuntimeException r) {
            logHelper.error("创建定时诊断任务 " + timingDiagnoseTask.getTestName() + "【失败】", null, null, r);

        }
        return new AddTimingTaskResponseParam();
    }

    /**
     * 创建定时诊断任务(41013)
     */
    @Transactional(rollbackFor = Exception.class)
    public ModifyTimingTaskResponseParam modifyTimingTask(ModifyTimingTaskRequestParam requestParam) {
        FdTimingDiagnoseStrategy timingDiagnoseTask = new FdTimingDiagnoseStrategy();
        try {
            BeanUtils.copyProperties(requestParam.getTimingDiagnoseTask(), timingDiagnoseTask);

            timing.delTiMing(timingDiagnoseTask.getId());

            strategyMapper.updateByPrimaryKeySelective(timingDiagnoseTask);

            timing.addTiming(timingDiagnoseTask.getStartTime(),
                    timingDiagnoseTask.getEndTime(),
                    timingDiagnoseTask.getCycle(),
                    timingDiagnoseTask.getCount(),
                    timingDiagnoseTask.getId());
            logHelper.normal("修改定时诊断任务:" + timingDiagnoseTask.getTestName() + "【成功】", null, null);
        } catch (RuntimeException r) {
            logHelper.error("修改定时诊断任务 " + timingDiagnoseTask.getTestName() + "【失败】", null, null, r);

        }
        return new ModifyTimingTaskResponseParam();
    }

    /**
     * 查询定时诊断任务(41014)
     */
    public QueryTimingTaskResponseParam queryTimingTask(QueryTimingTaskRequestParam requestParam) {
        QueryTimingTaskResponseParam responseParam = new QueryTimingTaskResponseParam();

        Example example = new Example(FdTimingDiagnoseStrategy.class);
        if (!StringUtils.isEmpty(requestParam.getId())) {
            example.createCriteria().andEqualTo("id", requestParam.getId());
        }
        List<FdTimingDiagnoseStrategy> timingDiagnoseStrategyList = strategyMapper.selectByExample(example);
        List<Fd_TimingDiagnoseStrategy> strategyList = BeansUtils.copyListProperties(timingDiagnoseStrategyList, Fd_TimingDiagnoseStrategy.class);
        responseParam.setTimingDiagnoseTaskList(strategyList);
        return responseParam;
    }

    /**
     * 删除定时诊断任务(41015)
     */
    @Transactional(rollbackFor = Exception.class)
    public DelTimingTaskResponseParam delTimingTask(DelTimingTaskRequestParam requestParam) {
        FdTimingDiagnoseStrategy diagnoseStrategy = new FdTimingDiagnoseStrategy();
        try {

            timing.delTiMing(requestParam.getId());
            diagnoseStrategy = strategyMapper.selectByPrimaryKey(requestParam.getId());
            strategyMapper.deleteByPrimaryKey(requestParam.getId());
            logHelper.normal("删除定时诊断任务:" + diagnoseStrategy.getTestName() + "【成功】", null, null);
        } catch (RuntimeException r) {
            logHelper.error("删除定时诊断任务 " + diagnoseStrategy.getTestName() + "【失败】", null, null, r);
        }
        return new DelTimingTaskResponseParam();
    }
}
